package com.xiaotu.spider;

import java.util.List;

import com.xiaotu.common.model.SpiderLogModel;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Request;

/**
 * @类名 AUrlPageProcessor
 * @日期 2017年2月8日
 * @作者 高海军
 * @功能 基于固定url爬取的处理类
 */
public abstract class AUrlPageProcessor extends APageProcessor
{
    public static final String URL_KEY = "urlKey";

    @Override
    protected Request getStartRequest(JSONObject target)
    {
        return null;
    }

    protected abstract String[] getStartRequestUrls();

    protected List<JSONObject> getStartRequestUrlsAndParams()
    {
        return null;
    }

    @Override
    public void setTargetList(List<JSONObject> targetList,
            SpiderLogModel spiderLog)
    {
        this.spiderLog = spiderLog;
        this.setSiteRequest();
    }

    @Override
    protected void setSiteRequest()
    {
        String[] urls = this.getStartRequestUrls();
        if (urls != null && urls.length > 0)
        {
            int i = urls.length;
            JSONObject target = this.getOtherTarget();
            for (String url : urls)
                this.addStartRequest(new Request(url), target, i--);
        }
        else
        {
            List<JSONObject> targetList = this.getStartRequestUrlsAndParams();
            if (targetList == null || targetList.isEmpty())
                return;
            int i = targetList.size();
            for (JSONObject target : targetList)
            {
                if (!target.has(URL_KEY))
                    continue;
                this.setOtherTarget(target);
                this.addStartRequest(new Request(target.getString(URL_KEY)), target, i--);
            }
        }

    }
}
